// NaniMoose's Gallery Code

var ArtDB = new Array();
var ZoomedIn = 1;
var ImgShown = -1;

var GlowPhase = 0;
var GlowDark = [219, 104, 56];
var GlowBright = [255, 175, 68];
var TargetScrollX = -1;
var ScrollVel = 0;
var ScrollFriction = 0.1;
var ScrollEffector = 0.3;

var artist = parent.Artists[parent.ThisArtist];
if (!(artist)) {
  if (parent.ThisArtist) {
    alert("No such artist: "+parent.ThisArtist);
  }
  document.location = "title.html";
}

var category = artist.categories[parent.ThisCategory];
if (!(category)) {document.location = "title.html";}

var ArtDirectory = category.path;
if (!(ArtDirectory)) {document.location = "title.html";}
ArtDirectory = "art/"+ArtDirectory;

var PrevOn   = new Image(); PrevOn.src   = "img/prev.png";
var PrevOff  = new Image(); PrevOff.src  = "img/prevoff.png";
var NextOn   = new Image(); NextOn.src   = "img/next.png";
var NextOff  = new Image(); NextOff.src  = "img/nextoff.png";
var ZoomFull = new Image(); ZoomFull.src = "img/full.png";
var ZoomFit  = new Image(); ZoomFit.src  = "img/fit.png";

function AddArt(FullPath, Title, Desc, Thumb, ForceWidth, ForceHeight) {
  m = FullPath.match(/^(.*)\/([^\/]+)\.(\w+)$/);
  if (m[1] != ArtDirectory) {return;}

  var a = ArtDB[ArtDB.length] = new Object();
  a.Title = Title;
  a.Desc = Desc;
  a.File = m[2];
  a.Ext = m[3];
  a.Thumb = a.File.replace(/ /g, "-");
  if (a.Thumb.length > 27) {a.Thumb = a.Thumb.substring(0, 27);}
  if (Thumb) {a.Thumb = Thumb;}
  a.num = ArtDB.length-1;
  a.img = 0;
  a.imgw = 0;
  a.imgh = 0;
  if ((ForceWidth > 0) && (ForceHeight > 0)) {
    a.imgw = ForceWidth;
    a.imgh = ForceHeight;
  }
}

function Init() {
  if (ArtDB.length == 0) {
    document.location = "title.html";
  }

  if ((!(document.getElementById))||(!(thumbs.document))) {
    alert("This gallery may not be compatible with your web browser.\nWe recommend IE 5.0+, FireFox 0.9+, Opera 6.0+, and Mozilla 1.0+.");
    return;
  }
  var c = '<table border="0" cellspacing="0" cellpadding="0"><tr>';
  for (var n=0; n<ArtDB.length; n++) {
    var a = ArtDB[n];
    c += '<td style="padding-left: 2px; padding-right: 2px;"><a href="javascript:parent.ShowImg('+n+')" title="'+a.Title+'"><img src="thumb/'+a.Thumb+'.jpg" class="Thumb" width="64" height="96"></a></td>';
  }
  c += '</tr></table>';

  e = thumbs.document.getElementById("iconrow");
  e.innerHTML = c;

  top.SetBar(parent.ThisArtist+",artist.html;"+category.title);

  window.setTimeout("ShowImg(0)", 100);
  window.setInterval('Glow()', 1000/30);
}

function ShowImg(n) {
  GlowPhase = Math.PI*3/2;
  if (ImgShown > -1) {
    thumbs.document.images[ImgShown].className = "Thumb";
  }
  thumbs.document.images[n].className = "ThumbSel";

  var e = bl.document.images[0];
  if (e) {e.src = (n>0) ? PrevOn.src : PrevOff.src;}
  e = bl.document.images[1];
  if (e) {e.src = (top.ZoomedIn == 1) ? ZoomFull.src : ZoomFit.src;}
  e = br.document.images[0];
  if (e) {e.src = (n<ArtDB.length-1) ? NextOn.src : NextOff.src;}

  var a = ArtDB[n];
  var c = '<center><div class="ImgTitle">'+a.Title+'</div>';
  if (a.Ext == "swf") {
    // For shockwave flash animations
    sc = ScaleImage(a.imgw, a.imgh, 1);

    c += '<table border="0"><tr><td class="ThumbSel">';
    c += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+sc.w+'" height="'+sc.h+'" id="movie" align=""><param name="movie" value="../'+ArtDirectory+"/"+a.File+'.swf"><embed src="../'+ArtDirectory+"/"+a.File+'.swf" quality="high" width="'+sc.w+'" height="'+sc.h+'" name="movie" align="" type="application/x-shockwave-flash" plug inspage="http://www.macromedia.com/go/getflashplayer"></object>';
    c += '</td></tr></table>';
  } else if ((a.imgw > 0) && (top.ZoomedIn != 1)) {
    // for cached images
    sc = ScaleImage(a.imgw, a.imgh);
    c += '<img src="../'+ArtDirectory+"/"+a.File+'.'+a.Ext+'" width="'+sc.w+'" height="'+sc.h+'" class="ImgDisp">';
  } else {
    // For uncached images
    c += '<img src="../'+ArtDirectory+"/"+a.File+'.'+a.Ext+'" onLoad="parent.ResizeImg('+n+')" class="ImgDisp">';
  }

  if (a.Desc != "") {
    c += '<div class="ImgDesc">'+a.Desc+"</div>";
  }

  c += '</center>';

  var e = display.document.getElementById("disp");
  e.innerHTML = c;
  ImgShown = n;
  display.window.scrollTo(0,0);
  CenterThumbs();

  PrebufferImg(n+1);
  PrebufferImg(n-1);
}

function DoNext() {
  if (ImgShown+1 < ArtDB.length) {
    ShowImg(ImgShown+1);
  }
}

function DoPrev() {
  if (ImgShown-1 > -1) {
    ShowImg(ImgShown-1);
  }
}

function ChangeZoom() {
  top.ZoomedIn = (top.ZoomedIn == 1) ? 0 : 1;
  ShowImg(ImgShown);
}

function DoExit() {
  document.location = "artist.html";
}

function ResizeImg(n) {
  i = display.document.images[0];
  if (!(i)) {return;}

  a = ArtDB[n];
  if ((n == ImgShown) && (a.imgw == 0)) {
    a.img = new Image();
    a.img.src = i.src;
    a.imgw = i.width;
    a.imgh = i.height;
  }

  if (top.ZoomedIn == 1) {return;}
  sc = ScaleImage(i.width, i.height);
  ShowImg(ImgShown);
}

function ScaleImage(iw, ih, maximize) {
  var w = GetW(display) - 8;
  var h = GetH(display) - 40;
  var r = w/h;
  var ir = iw/ih;
  if (r < ir) {
    nw = ((w<iw)||(maximize))?w:iw;
    nh = Math.floor(nw/ir);
  } else {
    nh = ((h<ih)||(maximize))?h:ih;
    nw = Math.floor(nh*ir);
  }
  r = new Object();
  r.w = nw;
  r.h = nh;
  return r;
}

function PrebufferImg(n) {
  if ((n<0) || (n>=ArtDB.length)) {return;}
  a = ArtDB[n];
  if (a.imgw > 0) {return;}
  a.img = new Image();
  a.img.src = "../"+ArtDirectory+"/"+a.File+'.'+a.Ext;
  a.img.onLoad = RescanImgs
}

function RescanImgs() {
  for (n=0; n<ArtDB.length; n++) {
    a = ArtDB[n];
    if (a.img) {
      if (a.imgw == 0) {
	if (a.img.width > 0) {
	  a.imgw = a.img.width;
	  a.imgh = a.img.height;
	}
      }
    }
  }
}


function CenterThumbs() {
  var scrw = GetW(thumbs);
  var totalw = thumbs.document.body.scrollWidth;
  var x = Math.round((totalw*ImgShown/ArtDB.length)-(scrw/2))+36;
  if (x<0) {x=0;}
  if (x>totalw-scrw) {x=totalw-scrw;}
  //thumbs.window.scrollTo(x,0);
  TargetScrollX = x;
}

function GetW(f) {
  var w = 700;
  if (f.window.innerWidth) {w = f.window.innerWidth;}
  else if (f.document.body) {
    if (f.document.body.offsetWidth) {
      w = f.document.body.offsetWidth;
    }
  }
  return w
}

function GetH(f) {
  var h = 400;
  if (f.window.innerHeight) {h = f.window.innerHeight;}
  else if (f.document.body) {
    if (f.document.body.offsetHeight) {
      h = f.document.body.offsetHeight;
    }
  }
  return h
}

function GetScrollX() {
  if (thumbs.document.body.scrollLeft) {
    return thumbs.document.body.scrollLeft;
  }
  if (thumbs.window.pageXOffset) {
    return thumbs.window.pageXOffset;
  }
  return 0;
}

function Glow() {
  GlowAmt = (-Math.sin(GlowPhase)+1)/2;
  GlowAnti = 1-GlowAmt;
  GlowPhase += 0.25;

  GlowCol = Array();
  for (var n=0; n<3; n++) {
    GlowCol[n] = Math.floor(GlowAmt*GlowBright[n] + GlowAnti*GlowDark[n]);
  }

  e = thumbs.document.styleSheets[0].cssRules;
  if (e) {
    for (var n=0; n<e.length; n++) {
      if (e[n].cssText.match(/\.ThumbSel/)) {
	e[n].style.borderColor = "rgb("+GlowCol.join(",")+")";
      }
    }
  }

  if (TargetScrollX > -1) {
    var x = GetScrollX();
    var originalx = x;
    ScrollVel *= ScrollFriction;
    ScrollVel += ScrollEffector * (TargetScrollX - x);
    x = Math.round(x+ScrollVel);
    if (Math.abs(x - TargetScrollX) < 2) {TargetScrollX = -1;}
    thumbs.window.scrollTo(x,0);
  } else {
    ScrollVel = 0;
  }
}
